﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LP580.CG
{
    /// <summary>
    /// clsFavoriteGiftProduct 的摘要说明
    /// <summary>
    public class clsFavoriteGiftProduct
    {
        #region 构造函数

        public clsFavoriteGiftProduct() { }

        #endregion


        #region 增删改数据库操作

        public static int Insert(clsFavoriteGiftProduct prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = string.Format("insert into [tbl_FavoriteGiftProduct](" +
                            " [UserID]," +
                            " [GiftProductID]" +
            ") values (" +
                    "@UserID," +
                    "@GiftProductID" +
            ")");
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@GiftProductID", prmEntity._giftproductid);
            SaveDTM.Insert();


            //对应产品收藏数增一
            clsGiftProduct.AddFavorite(prmEntity._giftproductid);


            return 1;
        }

        public static bool Delete(int prmUserID, int pid)
        {
            DBase dbo = new DBase();
            dbo.ExecSql("DELETE FROM [tbl_FavoriteGiftProduct] WHERE UserID=" + prmUserID.ToString() + " and GiftProductID=" + pid.ToString());
            dbo.CloseConn();
            dbo = null;

            //对应产品收藏数减一
            clsGiftProduct.DecFavorite(pid);

            return true;
        }

        #endregion

        /// <summary>
        /// 获取一个用户收藏了哪些产品
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static List<clsGiftProduct> GetCollectionByUser(int userid)
        {
            List<clsGiftProduct> ls = new List<clsGiftProduct>();
            DBase dbo = new DBase();
            string sql = "SELECT GiftProductID FROM [tbl_FavoriteGiftProduct] WHERE UserID=" + userid + " ORDER BY GiftProductID DESC";
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(new clsGiftProduct((int)dr[0]));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        /// <summary>
        /// 获取一个产品被哪些用户收藏
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static List<clsUserCompanyInfo> GetCollection(int giftproductid)
        {
            List<clsUserCompanyInfo> ls = new List<clsUserCompanyInfo>();
            DBase dbo = new DBase();
            string sql = "SELECT UserID FROM [tbl_FavoriteGiftProduct] WHERE GiftProductID=" + giftproductid + " ORDER BY UserID DESC";
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(new clsUserCompanyInfo((int)dr[0]));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        #region 字段属性

        private int _userid = 0;
        /// <summary>
        /// 所属用户
        /// </summary>
        public int UserID
        {
            get { return _userid; }
            set { _userid = value; }
        }

        private int _giftproductid = 0;
        /// <summary>
        /// 被收藏的礼品ID
        /// </summary>
        public int GiftProductID
        {
            get { return _giftproductid; }
            set { _giftproductid = value; }
        }

        #endregion
    }
}
